package java21demo;

import java.util.List;

enum Season {
    SPRING, SUMMER, AUTUMN, WINTER
}

enum Direction {
    EAST, WEST, SOUTH, NORTH
}

public class SwitchTest {
    public static void process(Object obj) {
        switch (obj) {
            case null -> System.out.println("null");
            case Person p -> System.out.println("name=" + p.name() + ", age=" + p.age());
            case String s -> System.out.println("String=" + s);
            case Integer i -> System.out.println("int=" + i);
            default -> System.out.println("未知对象");
        }
    }

    public static void nestedRecordPatternCaseTest(Person p) {
        switch (p) {
            case Person(String name, int age, Address(String c, String s, String z)) -> {
                System.out.println("name=" + name + ", age=" + age);
                System.out.println("city=" + c + ", streed=" + s);
            }
            default -> System.out.println("未知");
        }
    }

	public static void guardedLabelTest(Person p) {
        switch (p) {
            case null -> System.out.println("null");
            case Person(String name, int age, Address address) when age < 18 -> {
                System.out.println(name + "是未成年人!");
            }
            case Person(String name, int age, Address address)
                    when (name != null) && (name.length() > 2) -> { // age>=18时才会执行
                System.out.println(name + "的姓名长度 = " + name.length());
            }
            default -> System.out.println("其他情况");
        }
    }

    public static void enumConstantCaseTest(Object s) {
        switch (s) {
            case Season.SPRING -> System.out.println("春天");
            case Season.SUMMER -> System.out.println("夏天");
            case Season.AUTUMN -> System.out.println("秋天");
            case Season.WINTER -> System.out.println("冬天");
            case  Direction d -> System.out.println(d + "不是Season");
            default -> System.out.println("其他类型");
        }
    }
    public static void main(String[] args) {
        Person p = new Person("zhangsan", 18, new Address("xiamen", "jimei", "361001"));
        process(p);
        nestedRecordPatternCaseTest(p);
        Person p1 = new Person("Alice", 17, null);
        guardedLabelTest(p1); // 输出：alice是未成年人!
        Person p2 = new Person("ZhangSan", 19, null);
        guardedLabelTest(p2); // 输出：ZhangSan的姓名长度 = 8
        Person p3 = new Person("ET", 19, null);
        guardedLabelTest(p3); // 输出：其他情况

        enumConstantCaseTest(Season.AUTUMN);  // 输出：秋天
        enumConstantCaseTest(Direction.EAST); // 输出：EAST不是Season
        enumConstantCaseTest("abc");          // 输出：其他类型
    }
}
